var app = getApp();
var time= require("../../../utils/util.js");



Page({

  data: {
    lists: [],
    lists1:[],
    lists2:[],
    currentPage: 1,  
    pageSize: 6,     
    hideBottom: true,
    loadMore:'正在加载更多...',
    inputValue:'',
    ifsearch:false,
    stWidth:0,
    colOneHeight:0,
    colTwoHeight:0,
   
   
    
 
   

  },



  onShow: function () {
    // console.log("onshow执行开始");
    var that = this;
    var lists = that.data.lists;
    var lists1 = that.data.lists1;
    var lists2 = that.data.lists2;
    var stWidth = that.data.stWidth;
    lists=[];
    lists1=[];
    lists2=[];
    that.setData({
      lists: lists,
      lists1: lists1,
      lists2: lists2,
    });
    

    wx.getSystemInfo({
      success: (res) => {
        let ww = res.windowWidth;
        let stWidth = ww * 0.48;
        that.setData({
          stWidth: stWidth
        });
      }
    });
    wx.request({
      url: app.globalData.urlPath+"getGoodsList.action",
      data: {
        currentPage: 1,

        pageSize: 6,

      },
      method: "get",
      success: function (res) {


        for (var i = 0; i < res.data.length; i++) {
          res.data[i]["createTime"] = time.formatTime(res.data[i]["createTime"] / 1000, 'Y/M/D')
          res.data[i]["images"] = app.globalData.imgPath + res.data[i]["images"].split(";")[0];

          // console.log(res.data[i]);
          lists.push(res.data[i]);
        }

        that.setData({
          lists: lists,
          lists1: lists1,
          lists2: lists2,
          currentPage:1,
        });
        
        res.data.length= 0;
      },
    });
    // console.log(lists);
    // console.log(lists1);
    // console.log(lists2);

    // console.log("onshow执行完毕");
  },


 
  onImageLoad: function (options) {
    // console.log("onimageload执行");
    var that = this;
    var stWidth = that.data.stWidth;
    let currentId = options.currentTarget.id;
    let imageWidth = options.detail.width; // 图片原始宽度
    let imageHeight = options.detail.height; // 图片原始高度
    let scale= imageWidth/stWidth;
    let reHeight = imageHeight/scale;
    let lists = that.data.lists; // 获取所有的数据资源
    let lists1 = that.data.lists1;
    let lists2 = that.data.lists2;
    let colOneHeight = that.data.colOneHeight;
    let colTwoHeight = that.data.colTwoHeight;
    let listObj = null;
    // console.log("currentId:"+currentId);
    for (var i = 0; i < lists.length; i++) {
      // console.log("listid:"+lists[i].id);
      if (lists[i].id == currentId) {   
        listObj = lists[i];      
        break;
      }
    }
     

    // if (colTwoHeight <= colOneHeight) {
    //   colTwoHeight = colTwoHeight + reHeight;
    //   lists2.push(listObj)
    // } else {
    //   colOneHeight = colOneHeight + reHeight;
    //   lists1.push(listObj);

    // }



    if (colOneHeight <= colTwoHeight) {
      colOneHeight = colOneHeight+reHeight;
      lists1.push(listObj)
    } else {
      colTwoHeight =colTwoHeight+reHeight;
      lists2.push(listObj);
      
    }
    
  //  list1和list2排序 
    for (var j = 0; j < lists1.length - 1; j++) {    
      for (var i = 0; i < lists1.length - 1 - j; i++) {
        if (lists1[i].id > lists1[i + 1].id) {
          var temp = lists1[i];
          lists1[i] = lists1[i + 1];
          lists1[i + 1] = temp;
        }
      }
    }
    for (var j = 0; j < lists2.length - 1; j++) {
      for (var i = 0; i < lists2.length - 1 - j; i++) {
        if (lists2[i].id > lists2[i + 1].id) {
          var temp = lists2[i];
          lists2[i] = lists2[i + 1];
          lists2[i + 1] = temp;
        }
      }
    }
    

 

    that.setData({
    
      colOneHeight: colOneHeight,
      colTwoHeight: colTwoHeight,
      lists2: lists2,
      lists1: lists1,
    
    });
 
        // console.log(lists);
        // console.log(lists1);
        // console.log(lists2);

    lists = [];
    // console.log("onimageload结束");
  },




  jump: function (e) {
    var goodsId = e.currentTarget.dataset.id 
    // console.log(goodsId);
    wx.navigateTo({
      url: '../goodsDetail/goodsDetail?goodsId=' + goodsId,
    })
  },
  
  getValue: function (e) {
   
    this.setData({
      inputValue: e.detail.value
    });
  },

  jumpSearch:function() {
    var inputValue=this.data.inputValue;

   console.log("inputvalue:"+inputValue);
    var that = this;
    var lists = that.data.lists;
    var lists1 = that.data.lists1;
    var lists2 = that.data.lists2;
    
    lists=[];
    lists1=[];
    lists2=[];
    that.setData({
      lists: lists,
    });

   
            wx.request({
              url: app.globalData.urlPath + "getGoodsList.action",
              data: {
                currentPage: 1,
                pageSize: 6,
                value:inputValue,
              },
              method: "get",
              success: function (res) {
                  for (var i = 0; i < res.data.length; i++) {
                  
                    res.data[i]["createTime"] = time.formatTime(res.data[i]["createTime"] / 1000, 'Y/M/D')
                    res.data[i]["images"] = app.globalData.imgPath + res.data[i]["images"].split(";")[0];
                    // console.log(res.data[i]);
                    lists.push(res.data[i]);


                }
                if (res.data.length == 0) {
                  that.setData({
                    loadMore: '无相关商品'
                  })
                  }
                for (var j = 0; j < lists1.length - 1; j++) {
                  for (var i = 0; i < lists1.length - 1 - j; i++) {
                    if (lists1[i].id > lists1[i + 1].id) {
                      var temp = lists1[i];
                      lists1[i] = lists1[i + 1];
                      lists1[i + 1] = temp;
                    }
                  }
                }
                for (var j = 0; j < lists2.length - 1; j++) {
                  for (var i = 0; i < lists2.length - 1 - j; i++) {
                    if (lists2[i].id > lists2[i + 1].id) {
                      var temp = lists2[i];
                      lists2[i] = lists2[i + 1];
                      lists2[i + 1] = temp;
                    }
                  }
                }
                // console.log(lists);
                // console.log(lists1);
                // console.log(lists2);
                that.setData({
                  lists: lists,
                  lists1: lists1,
                  lists2: lists2,
                  currentPage:1, 
                  ifsearch: true,
                });
                res.data.length = 0;     


              },
            });
     
  

   

  },




  
 onReachBottom:function(){
      var that=this;
      var lists = that.data.lists;
      var lists1 = that.data.lists1;
      var lists2 = that.data.lists2;
      var ifsearch=that.data.ifsearch;
      var inputValue=that.data.inputValue;
      var currentPage=that.data.currentPage + 1
      lists = [];
      that.setData({
        currentPage:currentPage,
        hideBottom: false,
        lists:lists,
      })
     if (ifsearch = false){
      wx.request({
        
        url: app.globalData.urlPath + "getGoodsList.action",
        data: {
          currentPage: this.data.currentPage,
          pageSize: 6,
        },
        method: 'GET',
        success: function (res) {
            for (var i = 0;i < res.data.length; i++) {
              
              res.data[i]["createTime"] = time.formatTime(res.data[i]["createTime"] / 1000, 'Y/M/D')
              res.data[i]["images"] = app.globalData.imgPath + res.data[i]["images"].split(";")[0];
              // console.log(res.data[i]);
              lists.push(res.data[i]);
            }
              that.setData({
                lists: lists,                  
              })
          if (res.data.length==0) {
            that.setData({
              loadMore: '已经到底了'
            })
          }
          res.data=[];
          res.data.length=0;
        },                      
     });
       

     }
     else{
       var inputValue = this.data.inputValue;
       console.log(inputValue);

       that.setData({
         colOneHeight: 0,
         colTwoHeight: 0,
       })

       wx.request({

         url: app.globalData.urlPath + "getGoodsList.action",
         data: {
           currentPage: this.data.currentPage,
           pageSize: 6,
           value:inputValue,
           
         },
         method: 'GET',
         success: function (res) {


           for (var i = 0; i < res.data.length; i++) {
         
               res.data[i]["createTime"] = time.formatTime(res.data[i]["createTime"] / 1000, 'Y/M/D')
             res.data[i]["images"] = app.globalData.imgPath+ res.data[i]["images"].split(";")[0];
               // console.log(res.data[i]);
               lists.push(res.data[i]);
             
           }
           that.setData({

             lists: lists,
            
            
           })
           if (res.data.length == 0) {
             that.setData({
               loadMore: '已经到底了'
             })

           };
           res.data.length = 0;


         },
             complete: function () {
           wx.hideLoading();
         }


       })


     }

  },




})
